package com.kobeliu.api;

import com.kobeliu.api.interceptor.JwtCurrentUserInterceptor;
import com.kobeliu.api.interceptor.SMSInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class InterceptorConfig implements WebMvcConfigurer {

    /**
     * 在springboot容器中加入bean
     * @return
     */
    @Bean
    public SMSInterceptor smsInterceptor(){
        return new SMSInterceptor();
    }

    @Bean
    public JwtCurrentUserInterceptor jwtCurrentUserInterceptor(){
        return new JwtCurrentUserInterceptor();
    }

    /**
     * 注册拦截器 并且拦截指定路由 否则不生效
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(smsInterceptor())
                .addPathPatterns("/passport/getSMSCode");

        //所有请求都拦截
        registry.addInterceptor(jwtCurrentUserInterceptor())
                .addPathPatterns("/**");
    }

}
